// --------------------------------------------------------------------------------------------------------------------
// <copyright file="UIRootControl.cs" company="Open Trader">
//   Copyright (c) David Denis (david.denis@systemathics.com)
// </copyright>
// <summary>
//   |  Open Trader - The Open Source Systematic Trading Platform
//   |
//   |  This program is free software: you can redistribute it and/or modify
//   |  it under the terms of the GNU General Public License as published by
//   |  the Free Software Foundation, either version 2 of the License, or
//   |  (at your option) any later version.
//   |
//   |  This program is distributed in the hope that it will be useful,
//   |  but WITHOUT ANY WARRANTY; without even the implied warranty of
//   |  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   |  GNU General Public License for more details.
//   |
//   |  You should have received a copy of the GNU General Public License
//   |  along with this program.  If not, see http://www.gnu.org/licenses
//   |
//   |  Up to date informations about Open Trader can be found at :
//   |    http://opentrader.org
//   |    http://opentrader.codeplex.com
//   |
//   |  For professional services, please visit us at :
//   |    http://www.systemathics.com
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Org.OpenTrader.Framework.UI.Forms
{
    #region Using Directives

    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;

    using Org.OpenTrader.Framework.Enums;
    using Org.OpenTrader.Framework.Events;
    using Org.OpenTrader.Framework.Interfaces.UI;

    #endregion

    /// <summary>
    /// The ui root control.
    /// </summary>
    public partial class UIRootControl : UIControl, IUIRootControl
    {
        #region Constants and Fields

        /// <summary>
        /// The controls.
        /// </summary>
        private readonly IDictionary<string, IUIControl> controls = new Dictionary<string, IUIControl>();

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="UIRootControl"/> class.
        /// </summary>
        public UIRootControl()
        {
            this.InitializeComponent();

            this.AddControl(new UILiveObjectsRegistry(), "* Live Object Containers");

            ContainingFormEventCB += this.HandlerContainingFormCreated;
            UIForm.FormEventCB += this.HandlerFormEventCB;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="UIRootControl"/> class.
        /// </summary>
        /// <param name="name">
        /// The name.
        /// </param>
        public UIRootControl(string name)
        {
            this.Name = name;

            this.InitializeComponent();

            this.AddControl(new UILiveObjectsRegistry(), "* Live Object Containers");

            ContainingFormEventCB += this.HandlerContainingFormCreated;
            UIForm.FormEventCB += this.HandlerFormEventCB;
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// The run.
        /// </summary>
        /// <param name="title">
        /// The title.
        /// </param>
        public void Run(string title)
        {
            if (this.ContainingForm == null)
            {
                this.CreateContainingForm(title, EUIFormLayoutType.TitleBarAtTop, EUIFormClosingType.Hard);
            }

            if (this.ContainingForm != null && this.ContainingForm is Form)
            {
                this.ContainingForm.Visibility = EUIFormVisibility.Show;
                Application.Run(this.ContainingForm as Form);
            }
        }

        #endregion

        #region Implemented Interfaces

        #region IUIRootControl

        /// <summary>
        /// The add control.
        /// </summary>
        /// <param name="ctrl">
        /// The ctrl.
        /// </param>
        /// <param name="name">
        /// The name.
        /// </param>
        public void AddControl(IUIControl ctrl, string name)
        {
            if (!this.controls.ContainsKey(name))
            {
                var form = ctrl.CreateContainingForm(name, EUIFormLayoutType.TitleBarAtTop, EUIFormClosingType.Soft);
                form.Visibility = EUIFormVisibility.Hide;
                this.controls[name] = ctrl;
            }
        }

        /// <summary>
        /// The get control.
        /// </summary>
        /// <param name="name">
        /// The name.
        /// </param>
        /// <returns>
        /// </returns>
        public IUIControl GetControl(string name)
        {
            if (this.controls.ContainsKey(name))
            {
                return this.controls[name];
            }

            return null;
        }

        #endregion

        #endregion

        #region Methods

        /// <summary>
        /// The handler_ containing form created.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="args">
        /// The args.
        /// </param>
        protected void HandlerContainingFormCreated(object sender, FormEventArgs args)
        {
            this.UpdateAll();
        }

        /// <summary>
        /// The handler_ form created.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="args">
        /// The args.
        /// </param>
        protected void HandlerFormEventCB(object sender, FormEventArgs args)
        {
            this.UpdateAll();
        }

        /// <summary>
        /// The handler_ about.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void HandlerAbout(object sender, EventArgs e)
        {
            // AboutControl ctrl = new AboutControl();
        }

        /// <summary>
        /// The handler_ make form visible.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void HandlerMakeFormVisible(object sender, EventArgs e)
        {
            var sf = (sender as ToolStripMenuItem).Tag as UIForm;
            sf.Visibility = EUIFormVisibility.Show;
        }

        /// <summary>
        /// The handler_ make main form visible.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void HandlerMakeMainFormVisible(object sender, EventArgs e)
        {
            this.ContainingForm.Visibility = EUIFormVisibility.Show;
        }

        /// <summary>
        /// The handler_ ot website.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void HandlerOtWebsite(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start(@"http://opentrader.org");
        }

        /// <summary>
        /// The handler_ systemathics website.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void HandlerSystemathicsWebsite(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start(@"http://www.systemathics.com");
        }

        /// <summary>
        /// The update all.
        /// </summary>
        private void UpdateAll()
        {
            Devel.NoThrow(delegate { this.UpdateAllImpl(); });
        }

        /// <summary>
        /// The update all_ impl.
        /// </summary>
        private void UpdateAllImpl()
        {
            this.notifyIcon.Text = this.Name;

            this.UpdateWindowsSubMenu(this.nmenuStripWindows);
        }

        /// <summary>
        /// The update windows sub menu.
        /// </summary>
        /// <param name="windowsMenu">
        /// The windowsMenu.
        /// </param>
        private void UpdateWindowsSubMenu(ToolStripMenuItem windowsMenu)
        {
            if (this.ContainingForm == null)
            {
                return;
            }

            windowsMenu.DropDownItems.Clear();

            lock (UIForm.ExistingForms)
            {
                foreach (var sf in UIForm.ExistingForms)
                {
                    // don't put ourselve
                    if (sf != this.ContainingForm)
                    {
                        var subItem = new ToolStripMenuItem();

                        // subItem.Size = new System.Drawing.Size(61, 21);
                        subItem.Text = sf.Title;
                        subItem.Click += this.HandlerMakeFormVisible;
                        subItem.Tag = sf;
                        windowsMenu.DropDownItems.Add(subItem);
                    }
                }

                if (windowsMenu.DropDownItems.Count == 0)
                {
                    windowsMenu.Enabled = false;
                }
                else
                {
                    windowsMenu.Enabled = true;
                }
            }
        }

        #endregion
    }
}